package com.gui.login;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.controller.LoginCon;
import com.gui.admin.AdminFrame;
import com.gui.baseframe.BaseFrame;
import com.gui.info.InfoFrame;
import com.gui.user.UserIndex;

public class LoginAll extends BaseFrame{
	private static String userName=null;	//获取登录时的用户名
	//设置默认的字段名，选项卡名字
	public static final String REG_NAME="注册";
	public static final String LOG_NAME="登录";
	//新建字符串属性
	String title=null;
	JPanel jp_out_2=null;
	public LoginAll()
	{
		this.layoutComponents();	//调用主要组件方法
		this.init();	//调用窗体基本设置方法

	}
	public void layoutComponents()
	{
		//选项卡，放在最顶上
		JTabbedPane  jTabbed=new JTabbedPane(JTabbedPane.TOP) ;  
		//登录界面的方法
		JPanel p1=Login(LOG_NAME);
		//注册界面的方法
		JPanel p2=Login(REG_NAME);
		//添加入现象卡
		jTabbed.add("登录", p1);
		jTabbed.add("注册", p2);
		//添加组件到容器，放在中间
		this.add(jTabbed,BorderLayout.CENTER);
	}
	/**
	 * 用于用户登录或注册
	 */
	public JPanel Login(String title)
	{
		//界面容器
		JPanel jplog=new JPanel();
		//设置排列方式为：GriLayout
		jplog.setLayout(new GridLayout(3 ,1));
		//新建三个局部容器
		JPanel jp_in_gri_1 = new JPanel();
		JPanel jp_in_gri_2 = new JPanel();
		JPanel jp_in_gri_3 = new JPanel();
		ImageIcon background = new ImageIcon("src/image/background.jpg");  
		ImageIcon background2 = new ImageIcon("src/image/background2.jpg");  
        // 把背景图片显示在一个标签里面  
        JLabel label = new JLabel(background);  
        label.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				label.setIcon(background);
				jp_in_gri_1.add(label);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				label.setIcon(background2);
				jp_in_gri_1.add(label);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
			}
		});
		//组件入容器
		jp_in_gri_1.add(label);
		
		//用户名标签
		JLabel user=new JLabel("用户名:");
		//用户名填写文本框
		JTextField tuser=new JTextField(30);
		//添加组件
		jp_in_gri_2.add(user);
		jp_in_gri_2.add(tuser);
		
		if(title.equals("登录"))	
		{
			//密码标签
			JLabel pass=new JLabel("密    码:");
			//容器添加组件
			jp_in_gri_2.add(pass);
			
			//密码框
			JPasswordField password=new JPasswordField(30);
			//添加组件
			jp_in_gri_2.add(password);
			
			//登录按钮
			JButton loginbu=new JButton(title);
			//下拉列表框
			JComboBox<String> com=new JComboBox<String>();
			//设置下拉列表框中的值
			com.addItem("管理员");
			com.addItem("读者");
			
			//添加登录监听器
			loginbu.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String userstr=tuser.getText();		//获取用户输入的用户名值
					String passstr=password.getText();		//获取用户输入的密码值
					
					//如果用户名或密码为空
					if(user.equals("") || passstr.equals(""))
					{
						//弹窗
						JOptionPane.showMessageDialog(null, "用户名、密码不能为空", "警告",JOptionPane.WARNING_MESSAGE);
						return;
					}
					//如果下拉列表列表框的值为读者登录
					if(com.getSelectedIndex()==1){
						//调用读者登录的用户名密码判定方法
						if(LoginCon.userSel(userstr,passstr))
						{
							userName = userstr;
							new UserIndex();
							new InfoFrame();
							setVisible(false);
						}
					}
					else
					{
						if(LoginCon.adminSel(userstr, passstr))
						{
							userName = userstr;
							new AdminFrame();
							setVisible(false);
						}
					}
				}
			});
			//添加组件到容器
			jp_in_gri_3.add(com);
			jp_in_gri_3.add(loginbu);
		}
		else{
			//密码标签
			JLabel pass=new JLabel("密    码:");
			//添加组件
			jp_in_gri_2.add(pass);
			
			//密码输入框
			JPasswordField password=new JPasswordField(30);
			//添加组件
			jp_in_gri_2.add(password);
			
			JLabel passre=new JLabel("重写密码:");
			jp_in_gri_2.add(passre);
			//密码复写框
			JPasswordField passwordre=new JPasswordField(29);
			jp_in_gri_2.add(passwordre);
			
			//注册按钮
			JButton loginbu=new JButton(title);
			//取消按钮
			JButton exitbu=new JButton("取消");
			//下拉列表框
			JComboBox<String> com=new JComboBox<String>();
			com.addItem("管理员");
			com.addItem("读者");
			//注册按钮监听
			loginbu.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					String user=tuser.getText();	//获取用户名的值
					String pass=password.getText();	//获取密码的值
					String passre=passwordre.getText();	//获取重写密码的值
					
					//判断为空
					if(user.equals("") || pass.equals("") || passre.equals(""))
					{
						JOptionPane.showMessageDialog(null, "用户名、密码不能为空", "警告",JOptionPane.WARNING_MESSAGE); 
						return ;
					}
					
					//判断两次密码输入的值是否相同
					if(!(pass.equals(passre)))
					{
						JOptionPane.showMessageDialog(null, "两次输入的密码不相同", "警告",JOptionPane.WARNING_MESSAGE);
						password.setText("");
						passwordre.setText("");
						return ;
					}
					else
					{
						//成功注册，数据库写入数据
						if(com.getSelectedIndex()==1)
						{
							LoginCon.doIn(user, pass);
						}
						else
						{
							LoginCon.adminIn(user, pass);
						}
					}
				}
			});
			
			//添加组件
			jp_in_gri_3.add(com);
			jp_in_gri_3.add(loginbu);
			jp_in_gri_3.add(exitbu);
		}
		
		//添组件
		jplog.add(jp_in_gri_1);
		jplog.add(jp_in_gri_2);
		jplog.add(jp_in_gri_3);
		return jplog;
	}
	/**
	 * @return
	 * 		返回登录时输入的用户名
	 */
	public static String getUser()
	{
		return userName;
	} 
	/**
	 * 窗体基本设置
	 */
	public void init() 
	{
		super.init();
		this.setBounds(0, 0, 435,320);
		this.setLocationRelativeTo(null);
	}
}
